package com.qingcheng.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qingcheng.entity.Result;
import com.qingcheng.entity.SeckillStatus;
import com.qingcheng.service.seckill.SeckillOrderService;
import com.qingcheng.service.seckill.SeckillService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Welcome to the most Amazing IntelliJ IIIDEA.
 * Creat0r: ^_^ fffantianxing ^_^
 * Date: 2019-07-25
 * Time: 12:46
 */
@RestController
@RequestMapping("order")
public class SeckillOrderController {

    @Reference
    private SeckillOrderService seckillOrderService;

    /**
     * 下单功能
     * @param id
     * @return
     */
    @GetMapping("add")
    public Result add(String id,String time){

        try {
            String username = SecurityContextHolder.getContext().getAuthentication().getName();//获取当前用户登录名
            if ("anonymousUser".equals(username)){
                return new Result(403,"请登录！");
            }

            //调用接口生成订单
            boolean addFlag = seckillOrderService.add(id, time, username);

            if (addFlag){
                return new Result(0,"抢单成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(1,e.getMessage());
        }

        return new Result(1,"下单失败");

    }

    @GetMapping("queryStatus")
    public Result queryStatus(){
        String username = SecurityContextHolder.getContext().getAuthentication().getName();//获取当前用户登录名

        if (username.equalsIgnoreCase("anonymousUser")){
            return new Result(403,"请登录！");
        }

        SeckillStatus seckillStatus = seckillOrderService.querydStatus(username);
        if (seckillStatus!=null){
            return new Result(seckillStatus.getStatus(),"抢购状态");
        }

        return new Result(404,"没有抢购信息");
    }
}
